#include <lpc2292.h>
#include <RS232_driver.h>

//U0LCR - line control register's bits
#define SET_5bitDATA 0x00
#define SET_6bitDATA 0x01
#define SET_7bitDATA 0x02
#define SET_8bitDATA 0x03

#define SET_1stopBit 0x00
#define SET_2stopBit 0x04

#define ENABLE_PARITY_CNTR 0x08
#define DISABLE_PARITY_CNTR ~0x08

#define SET_PARITY_TYPE_ODD 0x00
#define SET_PARITY_TYPE_EVER 0x10

#define ENABLE_BREAK_CNTR 0x40
#define DISABLE_BREAK_CNTR ~0x40

#define ENABLE_DIV 0x80
#define DISABLE_DIV ~0x80 

//U0LSR - status register's bits
#define IS_READY 0x01 & U0LSR
#define RESET_IS_READY 0xfe

#define E_OVERFLOW 0x02 & U0LSR
#define E_PARRITY 0x04 & U0LSR
#define E_FORMAT 0x08 & U0LSR
#define E_ALL 0x80 & U0LSR

#define IS_BREAK 0x10 & U0LSR

#define STORAGE_IS_EMPTY 0x20 & U0LSR
#define SHIFT_IS_EMPTY 0x40 & U0LSR

//Initialize Rs232
void RS232Init()
{
	U0LCR = (SET_8bitDATA | SET_1stopBit | ENABLE_DIV) & DISABLE_PARITY_CNTR & DISABLE_BREAK_CNTR;
	
	U0DLL = 0x27;
	U0DLM = 0;
	
	U0LCR &= DISABLE_DIV;
}

//Get data from port
unsigned char RS232GetData(void)
{
	unsigned char inByte = 0;

    while (!IS_READY); 
    
	return U0RBR; 
}

//Send data to port
void RS232SendData(unsigned char data)
{
	while (!E_OVERFLOW); 
	
	U0THR = data; 
}